<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>做纯数字检测</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.ctype-digit" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ctype_digit</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">ctype_digit</span> &mdash; <span class="dc-title">做纯数字检测</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.ctype-digit-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>ctype_digit</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$text</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   检查提供的 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> 和 <code class="parameter">text</code> 里面的字符是不是都是数字。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.ctype-digit-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">text</code></dt>

     <dd>

      <p class="para">
       需要被测试的字符串。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.ctype-digit-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果 <code class="parameter">text</code> 字符串是一个十进制数字，就返回 <strong><code>TRUE</code></strong> ；反之就返回 <strong><code>FALSE</code></strong> 。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.ctype-digit-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.1.0</td>
       <td>
        在 PHP 5.1.0 之前，当 <code class="parameter">text</code> 是一个空字符串的时候，该函数将返回 <strong><code>TRUE</code></strong> 。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.ctype-digit-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6163">
    <p><strong>Example #1 一个 <span class="function"><strong>ctype_digit()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$strings&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'1820.20'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'10002'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'wsl!12'</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$strings&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$testcase</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">ctype_digit</span><span style="color: #007700">(</span><span style="color: #0000BB">$testcase</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;string&nbsp;</span><span style="color: #0000BB">$testcase</span><span style="color: #DD0000">&nbsp;consists&nbsp;of&nbsp;all&nbsp;digits.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;string&nbsp;</span><span style="color: #0000BB">$testcase</span><span style="color: #DD0000">&nbsp;does&nbsp;not&nbsp;consist&nbsp;of&nbsp;all&nbsp;digits.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string wsl!12 does not consist of all digits.
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-6164">
    <p><strong>Example #2 一个通过 <span class="function"><strong>ctype_digit()</strong></span> 来比对字符和整数的例子。</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$numeric_string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'42'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$integer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">ctype_digit</span><span style="color: #007700">(</span><span style="color: #0000BB">$numeric_string</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;true<br /></span><span style="color: #0000BB">ctype_digit</span><span style="color: #007700">(</span><span style="color: #0000BB">$integer</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;false&nbsp;(ASCII&nbsp;42&nbsp;is&nbsp;the&nbsp;*&nbsp;character)<br /><br /></span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$numeric_string</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;true<br /></span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$integer</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;true<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.ctype-digit-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    这个函数的参数要求是一个 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> 这一点是非常有用的，因此当你传入一个 <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>
    的参数也许不能得到期望的结果。然后，同样需要注意HTML表单将会返回数字字符串而不是一个整型。
    看下手册的 <a href="language.types.html" class="link">types</a> 章节。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">
如果给出一个 -128 到 255 之间(含)的整数, 将会被解释为该值对应的ASCII字符 (负值将加上 256 以支持扩展ASCII字符).
其它整数将会被解释为该值对应的十进制字符串.</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.ctype-digit-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="ctype_alnum.html" class="function" rel="rdfs-seeAlso">ctype_alnum()</a> - 做字母和数字字符检测</span></li>
    <li class="member"><span class="function"><a href="ctype_xdigit.html" class="function" rel="rdfs-seeAlso">ctype_xdigit()</a> - 检测字符串是否只包含十六进制字符</span></li>
    <li class="member"><span class="function"><a href="is_numeric.html" class="function" rel="rdfs-seeAlso">is_numeric()</a> - 检测变量是否为数字或数字字符串</span></li>
    <li class="member"><span class="function"><a href="is_int.html" class="function" rel="rdfs-seeAlso">is_int()</a> - 检测变量是否是整数</span></li>
    <li class="member"><span class="function"><a href="is_string.html" class="function" rel="rdfs-seeAlso">is_string()</a> - 检测变量是否是字符串</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>